<template>
  <contentModel v-if="modelValue" title="添加" @close="cancel">
    <a-form ref="formRef" :model="form" auto-label-width :rules="rules">
      <a-row :gutter="16">
        <a-col :span="8">
          <a-form-item field="secretLevel" label="密级">
            <secretLevelSelect
              v-model="form.secretLevel"
              style="width: 100%"
              :disabled="selectId"
              :secret-level="pmsReceiveRow.secretLevel"
            />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="packageNo" label="箱单号/运单号">
            <a-input v-model="form.packageNo" placeholder="请输入" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="invoiceNo" label="发票号">
            <a-input v-model="form.invoiceNo" placeholder="请输入" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="16">
        <a-col :span="8">
          <a-form-item field="deliverDate" label="发货日期">
            <a-date-picker
              v-model="form.deliverDate"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              style="width: 100%"
              placeholder="请选择"
            />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="deliverAddress" label="发货地点">
            <a-input v-model="form.deliverAddress" placeholder="请输入" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="netWeight" label="净重">
            <a-input-number v-model="form.netWeight" placeholder="请输入" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="16">
        <a-col :span="8">
          <a-form-item field="grossWeight" label="毛重">
            <a-input-number v-model="form.grossWeight" placeholder="请输入" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="freightFee" label="运费">
            <a-input-number v-model="form.freightFee" placeholder="请输入" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="insuranceFee" label="保费">
            <a-input-number v-model="form.insuranceFee" placeholder="请输入" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="16">
        <a-col :span="8">
          <a-form-item field="otherFee" label="杂费">
            <a-input-number v-model="form.otherFee" placeholder="请输入" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="totalFee" label="费用合计">
            <a-input-number v-model="form.totalFee" placeholder="请输入" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="goodsAmount" label="货物金额">
            <a-input-number v-model="form.goodsAmount" placeholder="请输入" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="16">
        <a-col :span="8">
          <a-form-item field="currencyType" label="币种">
            <dict-select
              v-model="form.currencyType"
              allow-clear
              dict-type="currencyType"
              placeholder="请选择"
            ></dict-select>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="packageVolume" label="包装体积">
            <a-input v-model="form.packageVolume" placeholder="请输入" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="transportType" label="发运方式">
            <dict-select
              v-model="form.transportType"
              allow-clear
              dict-type="shipVia"
              placeholder="请选择"
            ></dict-select>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="16">
        <a-col :span="8">
          <a-form-item field="receiveOrder" label="提货单号">
            <a-input v-model="form.receiveOrder" placeholder="请输入" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="receiveUserCode" label="接收人">
            <searchItemUser
              v-model="form.receiveUserCode"
              v-model:name="form.receiveUserName"
              :title="'接收人'"
            />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="receiveAddress" label="接收地点">
            <a-input v-model="form.receiveAddress" placeholder="请输入" />
          </a-form-item>
        </a-col>
      </a-row>

      <a-row :gutter="16">
        <a-col :span="8">
          <a-form-item field="receiveDate" label="接收日期">
            <a-date-picker
              v-model="form.receiveDate"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              style="width: 100%"
              placeholder="请选择"
            />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
    <template #footer>
      <a-space>
        <a-button type="outline" @click="handleCancel(record, rowIndex)"
          >取消</a-button
        >
        <a-button
          type="primary"
          :loading="loading"
          @click="handleSubmit(record, rowIndex)"
          >保存</a-button
        >
      </a-space>
    </template>
  </contentModel>
</template>

<script lang="ts" setup>
  import { defineAsyncComponent, inject, reactive, ref, toRefs } from 'vue';
  import dictSelect from '@/common/components/dict-select/index.vue';
  import { Message } from '@arco-design/web-vue';
  import {
    savePmsReceiveBox,
    editPmsReceiveBox,
    getPmsReceiveBoxDetailInfoById,
  } from '@/api/purchaseArrival/arrivalRegistration';
  // import dictSelect from '@/common/components/dict-select/index.vue';
  import searchItemUser from '@/common/components/search-modal/user.vue';
  import secretLevelSelect from '@/common/components/cac-components/secret-level-select/index.vue';

  const contentModel = defineAsyncComponent(
    () => import('@/common/components/content-model/index.vue')
  );
  const props = defineProps({
    modelValue: {
      type: Boolean,
      default: false,
    },
    // eslint-disable-next-line vue/require-prop-types
    pmsReceiveId: {},
    // eslint-disable-next-line vue/require-prop-types
    selectId: {},
  });
  const emits = defineEmits(['update:modelValue', 'fetchData']);
  const cancel = () => {
    emits('update:modelValue', false);
  };
  const pmsReceiveRow = inject('pmsReceiveRow');
  const { selectId, pmsReceiveId } = toRefs(props);
  // const { cancel } = useModelFun();
  const formRef = ref(null);
  const loading = ref(false);
  const form = reactive({});
  const rules = {
    secretLevel: [
      {
        required: true,
        message: '箱单号/运单号必填',
      },
    ],
    packageNo: [
      {
        required: true,
        message: '箱单号/运单号必填',
      },
    ],
    invoiceNo: [
      {
        required: true,
        message: '发票号必填',
      },
    ],
  };
  const handleSubmit = async () => {
    const validate = await formRef.value.validate();
    if (validate) return;
    form.pmsReceiveId = pmsReceiveId?.value;
    form.confirmFlag = 0;
    loading.value = true;
    try {
      if (form?.id) {
        await editPmsReceiveBox(form);
        Message.success({
          content: '编辑成功！',
          duration: 5 * 1000,
        });
      } else {
        await savePmsReceiveBox(form);
        Message.success({
          content: '保存成功！',
          duration: 5 * 1000,
        });
      }
      loading.value = false;
      cancel();
      emits('fetchData');
    } catch (error) {
      loading.value = false;
      console.log(error);
    }
  };
  // const handleOk = () => {
  //   cancel();
  // };
  const handleCancel = () => {
    cancel();
  };
  const getDetailInfo = async () => {
    const { data } = await getPmsReceiveBoxDetailInfoById(selectId?.value);
    Object.keys(data).forEach((key) => {
      form[key] = data[key];
    });
  };
  const init = () => {
    if (selectId?.value) {
      getDetailInfo();
    }
  };
  init();
</script>
